package com.xsq.newmall01.Controller;

import com.xsq.newmall01.Dao.*;
import com.xsq.newmall01.Service.SystemUserService.SystemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/system")
public class SystemController {

    @Autowired
    @Qualifier("systemService")
    private SystemService systemService;


    /**
     * 系统用户登录接口
     */
    @PostMapping("/login")
    public Map<String, Object> login(@RequestBody SystemUser systemUser) {
        Map<String, Object> result = new HashMap<>();
        try {
            // 调用服务层验证用户名和密码
            SystemUser loginUser = systemService.login(systemUser);
            if (loginUser != null) {
                result.put("success", true);
                result.put("data", loginUser);
                result.put("message", "登录成功");
            } else {
                result.put("success", false);
                result.put("message", "用户名或密码错误");
            }
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "登录失败：" + e.getMessage());
        }
        return result;
    }

    // 查看所有普通用户
    @GetMapping("/users")
    public Map<String, Object> getAllUsers() {
        Map<String, Object> result = new HashMap<>();
        try {
            List<User> users = systemService.getAllUsers();
            result.put("success", true);
            result.put("data", users);
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "查询失败：" + e.getMessage());
        }
        return result;
    }

    // 查看所有商家
    @GetMapping("/merchants")
    public Map<String, Object> getAllMerchants() {
        Map<String, Object> result = new HashMap<>();
        try {
            List<MerchantUser> merchants = systemService.getAllMerchants();
            result.put("success", true);
            result.put("data", merchants);
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "查询失败：" + e.getMessage());
        }
        return result;
    }

    // 修改商家状态
    @PutMapping("/merchants/status")
    public Map<String, Object> updateMerchantStatus(@RequestParam int id, @RequestParam int status) {
        Map<String, Object> result = new HashMap<>();
        try {
            systemService.updateMerchantStatus(id, status);
            result.put("success", true);
            result.put("message", "商家状态更新成功");
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "更新失败：" + e.getMessage());
        }
        return result;
    }

    // 修改普通用户状态
    @PutMapping("/users/status")
    public Map<String, Object> updateUserStatus(@RequestParam int id, @RequestParam int status) {
        Map<String, Object> result = new HashMap<>();
        try {
            systemService.updateUserStatus(id, status);
            result.put("success", true);
            result.put("message", "用户状态更新成功");
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "更新失败：" + e.getMessage());
        }
        return result;
    }

    // 查看所有订单
    @GetMapping("/orders")
    public Map<String, Object> getAllOrders() {
        Map<String, Object> result = new HashMap<>();
        try {
            List<Order> orders = systemService.getAllOrders();
            result.put("success", true);
            result.put("data", orders);
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "查询失败：" + e.getMessage());
        }
        return result;
    }

    // 查看所有商品
    @GetMapping("/goods")
    public Map<String, Object> getAllGoods() {
        Map<String, Object> result = new HashMap<>();
        try {
            List<Good> goods = systemService.getAllGoods();
            result.put("success", true);
            result.put("data", goods);
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "查询失败：" + e.getMessage());
        }
        return result;
    }

    // 管理员下架商品
    @DeleteMapping("/goods")
    public Map<String, Object> removeGoodByAdmin(@RequestParam int goodId) {
        Map<String, Object> result = new HashMap<>();
        try {
            systemService.removeGoodByAdmin(goodId);
            result.put("success", true);
            result.put("message", "商品下架成功");
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "下架失败：" + e.getMessage());
        }
        return result;
    }

    // 撤回订单
    @DeleteMapping("/orders/recall")
    public Map<String, Object> recallOrder(@RequestParam int orderId) {
        Map<String, Object> result = new HashMap<>();
        try {
            systemService.recallOrder(orderId);
            result.put("success", true);
            result.put("message", "订单撤回成功");
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "撤回失败：" + e.getMessage());
        }
        return result;
    }

    @PutMapping("/profile")
    public Map<String, Object> updateAdminProfile(@RequestBody SystemUser systemUser) {
        Map<String, Object> result = new HashMap<>();
        try {
            // 实现更新管理员信息的逻辑
            systemService.updateSystemUser(systemUser);
            result.put("success", true);
            result.put("message", "更新成功");
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "更新失败：" + e.getMessage());
        }
        return result;
    }

    @GetMapping("/users/{id}")
    public Map<String, Object> getUserById(@PathVariable int id) {
        Map<String, Object> result = new HashMap<>();
        try {
            User user = systemService.getUserById(id);
            result.put("success", true);
            result.put("data", user);
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "查询失败：" + e.getMessage());
        }
        return result;
    }

    @PutMapping("/users/update")
    public Map<String, Object> updateUserInfo(@RequestBody User user) {
        Map<String, Object> result = new HashMap<>();
        try {
            systemService.updateUserInfo(user);
            result.put("success", true);
            result.put("message", "用户信息更新成功");
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "更新失败：" + e.getMessage());
        }
        return result;
    }
}


